package cn.tedu.home1ess.admin.content.service.impl;

import cn.tedu.home1ess.admin.content.dao.repository.ITabooRepository;
import cn.tedu.home1ess.admin.content.pojo.param.TabooWordsParam;
import cn.tedu.home1ess.admin.content.pojo.po.TabooWordsCategorytPO;
import cn.tedu.home1ess.admin.content.pojo.po.TabooWordsContentPO;
import cn.tedu.home1ess.admin.content.pojo.vo.TabooWordsCategoryVO;
import cn.tedu.home1ess.admin.content.pojo.vo.TabooWordsVO;
import cn.tedu.home1ess.admin.content.service.ITabooService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TabooServiceImpl implements ITabooService {
    @Autowired
    private ITabooRepository tabooRepository;


    @Override
    public List<TabooWordsVO> listForContent() {
        //查找所有的内容
        List<TabooWordsContentPO> listForContent=tabooRepository.selectContent();
        //创建一个新的结果list
        List<TabooWordsVO> result=new ArrayList<>();
        //把po转换为我们要的结果,并根据类别id查找相应的类别
        for (TabooWordsContentPO po : listForContent) {
            TabooWordsVO tabooWordsVO = new TabooWordsVO();
            BeanUtils.copyProperties(po,tabooWordsVO);
            TabooWordsCategorytPO categorytPO=tabooRepository.selectCategoryById(po.getCategoryId());
            tabooWordsVO.setCategoryName(categorytPO.getName());
            tabooWordsVO.setSystemLevel(categorytPO.getSystemLevel());
            result.add(tabooWordsVO);
        }

        return result;
    }


    @Override
    public List<TabooWordsCategoryVO> listForCategories(Long parentId) {
        List<TabooWordsCategorytPO> list=tabooRepository.selectCategoryByParentId(parentId);
        List<TabooWordsCategoryVO> result=new ArrayList<>();
        for (TabooWordsCategorytPO tabooWordsCategorytPO : list) {
            TabooWordsCategoryVO tabooWordsCategoryVO = new TabooWordsCategoryVO();
            BeanUtils.copyProperties(tabooWordsCategorytPO,tabooWordsCategoryVO);
            result.add(tabooWordsCategoryVO);
        }
        return result;
    }

    @Override
    public void addTaboo(TabooWordsParam tabooWordsParam) {
        //判断禁忌词是修改还是新增,看传过来的参有没有id
        TabooWordsContentPO tabooWordsContentPO = new TabooWordsContentPO();

        BeanUtils.copyProperties(tabooWordsParam,tabooWordsContentPO);

        if (tabooWordsParam.getId()==null){
            //没有id,是新增

            //新增禁忌词默认都是启用的
            tabooWordsContentPO.setEnable(1);
            tabooWordsContentPO.setTwctCreate(new Date());
            tabooWordsContentPO.setTwctModified(new Date());
            tabooRepository.addTaboo(tabooWordsContentPO);
        }else {
            tabooWordsContentPO.setTwctModified(new Date());
            tabooRepository.updateTaboo(tabooWordsContentPO);
        }

    }

    @Override
    public TabooWordsVO updateCategoryContentById(Long id) {
        TabooWordsContentPO tabooWordsContentPO=tabooRepository.selectContentById(id);
        TabooWordsCategorytPO tabooWordsCategorytPO = tabooRepository.selectCategoryById(tabooWordsContentPO.getCategoryId());
        TabooWordsVO result=new TabooWordsVO();

        BeanUtils.copyProperties(tabooWordsContentPO,result);
        result.setCategoryName(tabooWordsCategorytPO.getName());
        Long parentId=tabooWordsCategorytPO.getParentId();
        result.setSystemLevel(parentId.toString());
        return result;
    }

    @Override
    public void deleteContentById(Long id) {
        tabooRepository.deleteContentById(id);
    }


}
